package com.yiji.fintech.musfinance.common.eventbus.listener;

import com.acooly.core.utils.enums.ResultStatus;
import com.yiji.fintech.musfinance.base.enums.NotifyStatusEnum;
import com.yiji.fintech.musfinance.center.base.result.ResultFacade;
import com.yiji.fintech.musfinance.common.context.SpringApplicationContext;
import com.yiji.fintech.musfinance.common.domain.base.BaseDomain;
import com.yiji.fintech.musfinance.common.domain.base.DepositDomain;
import com.yiji.fintech.musfinance.common.eventbus.event.base.BaseEvent;
import com.yiji.fintech.musfinance.common.eventbus.listener.base.AbstractCommonListener;
import com.yiji.fintech.musfinance.common.strategy.context.SendGeteWayContext;
import com.yiji.fintech.musfinance.common.strategy.gateway.SendGateWayDepositStrategy;
import com.yiji.fintech.musfinance.common.utils.cache.CacheConstant;
import com.yiji.fintech.musfinance.common.utils.cache.CacheService;
import com.yiji.fintech.musfinance.entity.InstBank;
import com.yiji.fintech.musfinance.service.InstBankService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Set;

/**
 * 充值通用监听器
 * Created by ouwen@yiji.com} on 2017/10/16.
 */
public class DepositListener<T extends BaseEvent,R extends BaseDomain> extends AbstractCommonListener<T,R>{

    @Autowired
    private CacheService cacheService;

    @Override
    protected void sendgetway(BaseDomain domain, ResultFacade result) {
        SendGateWayDepositStrategy depositStrategy = (SendGateWayDepositStrategy) SpringApplicationContext.getBeanByClass(SendGateWayDepositStrategy.class);
        SendGeteWayContext context = new SendGeteWayContext(depositStrategy);
        context.doSendGateWay(domain,result);
    }

    @Override
    protected void initTransaction(BaseDomain domain) {
        DepositDomain depositDomain = (DepositDomain)domain;
        //初始化收款方银行信息
        Set<InstBank>  instBanks = cacheService.doQueryMutiCache(CacheConstant.CENTER_INSTBANK_MUTICACHE, InstBank.class, InstBankService.class);
        InstBank instBank = instBanks.parallelStream().filter((i)->i.getInstBankId().equals(depositDomain.getRecBankId())).findFirst().orElse(null);
        if(instBank != null){
            depositDomain.setRecBankName(instBank.getInstBankName());
            depositDomain.setRecUnionBankNo(instBank.getUnionBankNo());
        }
        depositDomain.setStatus(ResultStatus.processing);
        depositDomain.setNotifyStatus(NotifyStatusEnum.NO);
        depositDomain.setGatewayTime(new Date());
    }

    @Override
    public void doExcute(T event) {
        super.doExcute(event);
    }
}
